package com.yuelan.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuelan.common.core.utils.DateUtils;
import com.yuelan.common.core.utils.uuid.IdWorker;
import com.yuelan.common.core.web.page.TableDataInfo;
import com.yuelan.order.domain.RentBill;
import com.yuelan.order.mapper.RentBillMapper;
import com.yuelan.order.domain.RentPayOrder;
import com.yuelan.order.mapper.RentPayOrderMapper;
import com.yuelan.order.service.RentOrderService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.util.List;

import static com.yuelan.order.common.BillType.Collected;

/**
 * @description:
 * @Author: ZhaoYi
 * @date: 24-6-25 14:13
 */
@Service
public class RentOrderServiceImpl extends ServiceImpl<RentPayOrderMapper, RentPayOrder> implements RentOrderService  {

    @Resource
    private RentPayOrderMapper rentPayOrderMapper;
    @Resource
    private RentBillMapper rentBillMapper;


    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @Async(value = "taskRentOrderExecutor")
    public void createRentPayOrder(RentPayOrder rentPayOrder) {
        RentPayOrder orderInfo = rentPayOrderInfo(rentPayOrder);
        rentPayOrderMapper.insert(orderInfo);
        // 更新账单状态
        LambdaUpdateWrapper<RentBill> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(RentBill::getId, rentPayOrder.getId());
        updateWrapper.set(RentBill::getStatus, Collected.getStatus());
        rentBillMapper.update(updateWrapper);
    }

    private RentPayOrder rentPayOrderInfo(RentPayOrder rentPayOrder) {
        RentPayOrder payOrder = new RentPayOrder();
        payOrder.setId(IdWorker.snowflake());
        payOrder.setUserId(rentPayOrder.getUserId());
        payOrder.setPaymentMethod("微信支付");
        payOrder.setPaymentStatus("success");
        payOrder.setPaymentAmount(rentPayOrder.getPaymentAmount());
        payOrder.setPaymentDate(DateUtils.getNowDate());
        payOrder.setCreatedAt(DateUtils.getNowDate());
        payOrder.setUpdatedAt(DateUtils.getNowDate());
        return payOrder;
    }

}
